import {core, getGameObjectById, ids, SpriteRenderer, Transform} from "../gameengine";
import {backPackPrefab, iconPrefab} from "../data/data";
import {IconBehaviour} from "./icon";
import {user} from "../user";

export const keyDescriptionsDictionary: { [url: string]: string } = {};
export const keyChineseNameDictionary: { [url: string]: string } = {};
export const keyPropertyDictionary: { [url: string]: string } = {};
export const keyPreviewDictionary: { [url: string]: string[] } = {};

const notesPreview = [
    "assets/picture/preview/chapter1.png",
    "assets/picture/preview/chapter4.png",
    "assets/picture/preview/chapter6.png",
    "assets/picture/preview/chapter7.png",
    "assets/picture/preview/chapter9.png",
];
keyPreviewDictionary["notes"] = notesPreview;

const diaryPreview = [
    "assets/picture/preview/diary_content.png"
];
keyPreviewDictionary["diary"] = diaryPreview;

const noteManPreview = [
    "assets/picture/preview/noteMan_content.png"
];
keyPreviewDictionary["noteMan"] = noteManPreview;


export class BackPack {
    public objsInBackPack: string[];
    private isShowing = false;

    constructor() {
        this.loadKeyDescriptions();
        window.addEventListener("keydown", (ev) => {
            if (ev.code == "KeyB") {
                if (!this.isShowing) {
                    this.showBackpack();
                } else {
                    this.hideBackPack();
                }
            }
        });
    }

    public loadKeyDescriptions() {
        for (var i = 0; i < keyData.length; i++) {
            keyChineseNameDictionary[keyData[i].name] = keyData[i].chineseName;
            keyDescriptionsDictionary[keyData[i].name] = keyData[i].description;
            keyPropertyDictionary[keyData[i].name] = keyData[i].property;
        }
    }

    public showBackpack() {

        this.objsInBackPack=user.keys;
        this.isShowing = true;
        const prefab = core.createPrefab(backPackPrefab);
        const scene = getGameObjectById("Container");
        scene.addChild(prefab);

        for (var i = 0; i < this.objsInBackPack.length; i++) {
            const icon = core.createPrefab(iconPrefab);
            icon.id=this.objsInBackPack[i]+"icon";

            ids[icon.id]=icon;

            console.log(icon);
            prefab.addChild(icon);
            const spriteRender = icon.getScript(SpriteRenderer);

            let isUsed: boolean = false;
            console.log("user.useBagItem");
            console.log(user.useBagItems);
            for (var j=0;j<user.useBagItems.length;j++) {
                console.log("this.objsInBackPack[i]");
                console.log(this.objsInBackPack[i]);
                console.log("objName");
                console.log(user.useBagItems[j]);
                if (this.objsInBackPack[i] == user.useBagItems[j]){
                    isUsed = true;
                }

            }

            console.log(isUsed);

            if (isUsed) {
                spriteRender.imageName = "assets/picture/backpack/" + this.objsInBackPack[i] + "_grey.png";
            } else {
                spriteRender.imageName = "assets/picture/backpack/" + this.objsInBackPack[i] + ".png";
            }


            const transform = icon.getScript(Transform);
            transform.x += (i % 8) * 59;
            transform.y += Math.floor(i / 8) * 58;

            const iconBehaviour = icon.getScript(IconBehaviour);
            iconBehaviour.objname = this.objsInBackPack[i];
        }
    }

    public hideBackPack() {
        this.isShowing = false;
        const scene = getGameObjectById("Container");
        const prefab = getGameObjectById("backPackPrefab");
        scene.removeChild(prefab);
    }
}

const keyData = [
    {
        name: "crazyWord",
        property: "Normal",
        chineseName: "疯子的话",
        description: "“猫儿闹，鱼儿叫，珍珠在睡觉，蓝色的血，在管道深处，人\n们都需要。”"
    }, {
        name: "E",
        property: "Normal",
        chineseName: "图片碎片“E”",
        description: "图片碎片，上面模模糊糊写着一个字母\"E\"，字上长出了小\n吸盘"
    }, {
        name: "fire",
        property: "Normal",
        chineseName: "打火机",
        description: "一个打火机，看上去有些年头了"
    }, {
        name: "candle1",
        property: "Normal",
        chineseName: "海底蜡烛1",
        description: "一根蜡烛，看久了仿佛在桌子上蠕动。"
    }, {
        name: "knife",
        property: "Normal",
        chineseName: "剖鱼刀",
        description: "带“E”的剖鱼刀"
    }, {
        name: "ironKey",
        property: "Normal",
        chineseName: "铁质钥匙",
        description: "铁质钥匙"
    }, {
        name: "diary",
        property: "Special",
        chineseName: "一本日记",
        description: "一本日记"
    }, {
        name: "bag",
        property: "Normal",
        chineseName: "装尸体的袋子",
        description: "黑色袋子，散发着尸体的腐臭味"
    }, {
        name: "gun",
        property: "Normal",
        chineseName: "手枪",
        description: "一把手枪，锈迹斑斑，似乎还有一发子弹。"
    }, {
        name: "R",
        property: "Normal",
        chineseName: "图片碎片“R”",
        description: "图片碎片R，似曾相识的记号，仿佛一直沉睡在海底的深谷里。"
    }, {
        name: "noteMan",
        property: "Special",
        chineseName: "渔夫的字条",
        description: "一个字条，歪歪扭扭像是渔夫的字："
    }, {
        name: "goldenKey",
        property: "Normal",
        chineseName: "金钥匙",
        description: "金钥匙，似乎与大门口的锁孔对应，要去试试吗？"
    }, {
        name: "stone",
        property: "Normal",
        chineseName: "铺满绿泥的石头",
        description: "铺满绿泥的石头，好像在哪里见过，像是从某个腐败的古城\n堡墙壁上脱落的。泥泞正覆盖着流水冲刷出的波纹。 "
    }, {
        name: "notes",
        property: "Special",
        chineseName: "笔记残页",
        description: "一些笔记残页，似乎记录着很重要的东西"
    }, {
        name: "candle2",
        property: "Normal",
        chineseName: "海底蜡烛2",
        description: " 一根蜡烛，像扔掉的死鱼，或者说睡着了的。"
    }, {
        name: "statue",
        property: "Normal",
        chineseName: "上古雕像",
        description: " 一个雕像，若说是被扔到了黑暗的缝隙中，不如说是它自己藏\n到了这里。仔细看的话似乎刻着一些符号。"
    },
];

export const backPack = new BackPack();